import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useDataStore = defineStore(
  'data',
  () => {
    const data = ref([])
    const setEmptyData = () => {
      data.value = []
    }
    const getData = computed(() => data.value)
    const setData = (newData) => {
      data.value = newData
    }
    const pushData = (newData) => {
      data.value.push(newData)
    }
    const removeData = (number) => {
      data.value.forEach((item, index) => {
        if (item['学号'] == number) {
          data.value.splice(index, 1)
        }
      })
    }
    const searchData = (key, value, page, size) => {
      let filterKey = data.value.filter((item) => {
        if (item[key]) {
          return String(item[key]).includes(value)
        }
      })
      if (key == '姓名') {
        let filterNum = data.value.filter((item) => {
          return String(item['学号']).includes(value) && !String(item['姓名']).includes(value)
        })
        filterKey.push(...filterNum)
      }
      let index = (page - 1) * size
      let list = filterKey.slice(index, index + size)
      return list
    }
    return { data, setEmptyData, getData, pushData, removeData, searchData }
  },
  {
    persist: true
  }
)
